<template>
    <div>
        <span id="headImg">头像</span>

        <el-upload
                class="avatar-uploader"
                :http-request="myupload"
                action
                :auto-upload="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
                name="file"
                ref="my"
                limit="1"
        >
            <img id="showHead"  v-if="imageUrl" :src="imageUrl" class="avatar">
            <i style="width: 20px; height: 20px; position: relative; top:-30px; left: 30px" v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>



    </div>


</template>
//
<script>
    import {request} from "../../network/request";

    export default {
        data() {
            return {
                imageUrl: sessionStorage.getItem("imgUrl"),
                Existence:false
            };
        },
        methods: {
            remind(){
              alert();
            },
            handleAvatarSuccess(res, file) {
               // alert();
                console.log(this.imageUrl);
                this.imageUrl = URL.createObjectURL(file.raw);
            },
            beforeAvatarUpload(file) {
              //  alert();
                if(this.existence){
                    this.$message.error('不能重复上传');
                    return false;
                }
                this.existence = true;
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            uploading(){
                this.$refs.my.submit()
            },
            myupload(content){
                //console.log(content.file)
                let myformData = new FormData()
                myformData.append('file',content.file);
                let userId = parseInt(sessionStorage.getItem("userId"));
                myformData.append("id", userId);
                request({
                    url:"/fileUpload",
                    method: "post",
                    data:myformData

                }).then(res=>{
                    //alert();
                    console.log(22)
                    console.log(res.data);
                    sessionStorage.setItem("imgUrl", res.data);
                    this.imageUrl = res.data;
                    //alert(res.data);

                }).catch(err=>{
                    //alert(err);
                    console.log(err);
                });

            }
        },
        mounted() {
            document.getElementsByClassName(".el-upload")[0].style.border = "1px dashed #d9d9d9";
        }
    }
</script>

<style scoped>
    #headImg{
        text-align: right;
        vertical-align: middle;
        font-size: 14px;
        color: #606266;
        line-height: 40px;
        padding: 0 12px 0 0;
        box-sizing: border-box;
        position: relative;
        left: 60px;
        top: 80px;
    }
    #showHead{
        height: 120px;
        width: 120px;
        padding: 10px;
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;

    }
    #showHead:hover{
        border-color: #409EFF;
    }
    .avatar-uploader  {
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        width: 170px;
        margin-left: 100px;
    }
    .avatar-uploader:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>